#include "DVRCommonPCH.h"
#include "Transform.h"

using namespace hiveDVR;

//**************************************************************************************************
//FUNCTION:
glm::mat4 CTransform::update(const glm::vec3& vTranslationFactor, const glm::vec3& vScalingFactor, const glm::vec2& vRotationFactor)
{
	// TODO: cannot scale the object now maybe because it is a unit bounding box now
	m_Matrix = __updateTranslation(vTranslationFactor)
		* __updateScaling(vScalingFactor)
		* __updateRotation(vRotationFactor)
		* m_Matrix;

	return m_Matrix;
}

//**************************************************************************************************
//FUNCTION:
glm::mat4 CTransform::__updateTranslation(const glm::vec3& vTranslationFactor)
{
	return glm::translate(glm::mat4(1.0f), vTranslationFactor);
}

//**************************************************************************************************
//FUNCTION:
glm::mat4 CTransform::__updateScaling(const glm::vec3& vScalingFactor)
{
	return glm::scale(glm::mat4(1.0f), vScalingFactor);
}

//**************************************************************************************************
//FUNCTION:
glm::mat4 CTransform::__updateRotation(const glm::vec2& vRotationFactor, float vRotationSpeed)
{
	if (vRotationFactor.x == 0.0f && vRotationFactor.y == 0.0f) return glm::mat4(1.0f);

	glm::quat Rotation = glm::quat(1, 0, 0, 0);
	Rotation = glm::angleAxis(vRotationFactor.x * vRotationSpeed, glm::vec3(glm::rotate(Rotation, glm::vec4(0, 1, 0, 0)))) * Rotation;
	Rotation = glm::angleAxis(vRotationFactor.y * vRotationSpeed, glm::vec3(glm::rotate(Rotation, glm::vec4(1, 0, 0, 0)))) * Rotation;
	Rotation = glm::normalize(Rotation);

	return glm::toMat4(Rotation);
}